﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Thesis_v01
{
   public partial class EditLesson : Form
   {
      //private CourseList2 courseList;
      private int lesson;
      private int selectedIndex;
      private int selectedQuestionID;
      private int selectedQuestionNum;
      private ArrayList questions;

      public EditLesson(int selectedLesson, ArrayList existingQuestions, string lessonName)
      {
         InitializeComponent();

         //courseList = cList;
         lesson = selectedLesson;
         questions = existingQuestions;
         this.Text = "Editing lesson (" + lessonName + ")";

         //ListViewItem item = listView1.Items.Add("1");
         //item.Checked = true;
         //item.SubItems.AddRange(new string[] {
         //   "MCQ",
         //   "Blah blah blah?"});
      }

      public void ListExistingQuestions(ArrayList questionDetails)
      {
         listView1.Items.Clear();

         foreach (QuestionDetail questions in questionDetails)
         {
            ListViewItem tempItem = listView1.Items.Add(questions.Number.ToString());
            tempItem.SubItems.AddRange(new string[] {
               questions.Type.ToString(),
               questions.Question});
         }

         listView1.Refresh();
      }

      private void addQuestionToolStripButton_Click(object sender, EventArgs e)
      {
         AddEditQuestion addQuestion = new AddEditQuestion(true);
         addQuestion.MdiParent = this.MdiParent;
         addQuestion.Text = "Add Question";
         addQuestion.Show();

         MainApp main = (MainApp)this.MdiParent;
         main.ToggleEditLessonEnable();
      }

      public void AddQuestion(QuestionDetail question)
      {
         //QuestionDetail lastQuestion = (QuestionDetail)questions[questions.Count - 1];
         //int questionNumber = lastQuestion.Number;
         int questionNumber = FindLastQuestionNumber();
         question.Number = questionNumber + 1;
         question.Class = lesson;

         DBConnection dbConn = new DBConnection();
         question.ID = dbConn.InsertQuestion(question);
         questions.Add(question);

         ListExistingQuestions(questions);
      }

      private void editQuestionToolStripButton_Click(object sender, EventArgs e)
      {
         selectedIndex = listView1.SelectedIndices[0];
         QuestionDetail selectedQuestion = (QuestionDetail)questions[selectedIndex];
         selectedQuestionID = selectedQuestion.ID;
         selectedQuestionNum = selectedQuestion.Number;

         AddEditQuestion editQuestion = new AddEditQuestion(false);
         editQuestion.ExistingQuestion = selectedQuestion;
         editQuestion.MdiParent = this.MdiParent;
         editQuestion.Text = "Edit Question";
         editQuestion.LoadExistingQuestion();
         editQuestion.Show();

         MainApp main = (MainApp)this.MdiParent;
         main.ToggleEditLessonEnable();
      }

      public void EditQuestion(QuestionDetail question)
      {
         question.ID = selectedQuestionID;
         question.Class = lesson;
         question.Number = selectedQuestionNum;
         questions[selectedIndex] = question;

         DBConnection dbConn = new DBConnection();
         dbConn.UpdateQuestion(question);

         ListExistingQuestions(questions);
      }

      private void removeQuestionToolStripButton_Click(object sender, EventArgs e)
      {
         selectedIndex = listView1.SelectedIndices[0];
         QuestionDetail selectedQuestion = (QuestionDetail)questions[selectedIndex];

         DialogResult result = MessageBox.Show("Are you sure you want to delete the selected question?", 
            "Comfirm detetion...", 
            MessageBoxButtons.YesNo, 
            MessageBoxIcon.Warning, 
            MessageBoxDefaultButton.Button2);

         if (result == DialogResult.Yes)
         {
            DBConnection dbConn = new DBConnection();
            dbConn.DeleteQuestion(selectedQuestion);

            questions.RemoveAt(selectedIndex);
            listView1.Items.RemoveAt(selectedIndex);
         }
      }

      private void listView1_SelectedIndexChanged(object sender, EventArgs e)
      {
         if (listView1.SelectedIndices.Count > 0)
         {
            editQuestionToolStripButton.Enabled = true;
            removeQuestionToolStripButton.Enabled = true;
         }
         else
         {
            editQuestionToolStripButton.Enabled = false;
            removeQuestionToolStripButton.Enabled = false;
         }
      }

      private int FindLastQuestionNumber()
      {
         int questionNum = 0;

         foreach (QuestionDetail question in questions)
         {
            if (question.Number > questionNum)
            {
               questionNum = question.Number;
            }
         }

         return questionNum;
      }

      private void EditLesson_FormClosing(object sender, FormClosingEventArgs e)
      {
         MainApp main = (MainApp) this.MdiParent;
         main.ToggleCourseListEnable();
      }
   }
}
